package com.jhhc.FloodForecast.Sensor.ServiceImpl;


import com.jhhc.BDMS.Data.Pojo.StationBO.SationInfo;
import com.jhhc.BDMS.Data.Pojo.StationBO.SensorConfig;
import com.jhhc.FloodForecast.Sensor.Dao.BDMS_ST_SENSOR_BMapper;
import com.jhhc.FloodForecast.Sensor.Dao.BDMS_ST_STBPRP_BMapper;
import com.jhhc.FloodForecast.Sensor.Pojo.BDMS_ST_SENSOR_B;
import com.jhhc.FloodForecast.Sensor.Pojo.BDMS_ST_STBPRP_B;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_STBPRP_BService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("BDMS_ST_STBPRP_BService")
public class BDMS_ST_STBPRP_BServiceImpl implements BDMS_ST_STBPRP_BService {

    @Autowired
    private BDMS_ST_STBPRP_BMapper bdms_st_stbprp_bMapper;

    @Autowired
    private BDMS_ST_SENSOR_BMapper sensor_bMapper;



    @Override
    public List<BDMS_ST_STBPRP_B> selectStationByProject(Integer Project) {
        return bdms_st_stbprp_bMapper.selectStationByProject(Project);
    }

    @Override
    public List<BDMS_ST_STBPRP_B> selectByPid(int projectId, List<String> stationCodes) {
        return bdms_st_stbprp_bMapper.selectByPid(projectId,stationCodes);
    }

    @Override
    public List<SationInfo> getStationInfos(int projectId, String[] sttp,List<String> sensorTypes) {
        List<BDMS_ST_STBPRP_B> bList = bdms_st_stbprp_bMapper.getStationInfos(projectId, sttp);
        List<BDMS_ST_SENSOR_B> configList = sensor_bMapper.getStationInfos(projectId, sensorTypes);
        if (bList != null && bList.size() > 0) {
            List<SationInfo> sationInfos = new ArrayList<>();
            SationInfo sationInfo = null;
            for (BDMS_ST_STBPRP_B bdmsStStbprpB : bList) {
                sationInfo = new SationInfo();
                //sationInfo.setProjectId(projectId);
                if (bdmsStStbprpB.getStnm() != null && !"".equals(bdmsStStbprpB.getStnm())) {
                    sationInfo.setStationName(bdmsStStbprpB.getStnm());
                }
                if (bdmsStStbprpB.getStcd() != null && !"".equals(bdmsStStbprpB.getStcd())) {
                    sationInfo.setStationCode(bdmsStStbprpB.getStcd());
                }
                if (bdmsStStbprpB.getRvnm() != null && !"".equals(bdmsStStbprpB.getRvnm())) {
                    sationInfo.setRiverName(bdmsStStbprpB.getRvnm());
                }
                if (bdmsStStbprpB.getHnnm() != null && !"".equals(bdmsStStbprpB.getHnnm())) {
                    sationInfo.setHnnm(bdmsStStbprpB.getHnnm());
                }
                if (bdmsStStbprpB.getBsnm() != null && !"".equals(bdmsStStbprpB.getBsnm())) {
                    sationInfo.setBasinName(bdmsStStbprpB.getBsnm());
                }
                if (bdmsStStbprpB.getLgtd() != null && !"".equals(bdmsStStbprpB.getLgtd())) {
                    sationInfo.setLgtd(bdmsStStbprpB.getLgtd());
                }
                if (bdmsStStbprpB.getLttd() != null && !"".equals(bdmsStStbprpB.getLttd())) {
                    sationInfo.setLttd(bdmsStStbprpB.getLttd());
                }
                if (bdmsStStbprpB.getDtmnm() != null && !"".equals(bdmsStStbprpB.getDtmnm())) {
                    sationInfo.setDtmnm(bdmsStStbprpB.getDtmnm());
                }
                if (bdmsStStbprpB.getDtmel() != null && !"".equals(bdmsStStbprpB.getDtmel())) {
                    sationInfo.setDtmel(bdmsStStbprpB.getDtmel());
                }
                if (bdmsStStbprpB.getDrna() != null && !"".equals(bdmsStStbprpB.getDrna())) {
                    sationInfo.setDrna(bdmsStStbprpB.getDrna());
                }
                if (bdmsStStbprpB.getDtpr() != null && !"".equals(bdmsStStbprpB.getDtpr())) {
                    sationInfo.setDtpr(bdmsStStbprpB.getDtpr());
                }
                if (bdmsStStbprpB.getStnm() != null && !"".equals(bdmsStStbprpB.getStnm())) {
                    sationInfo.setStationName(bdmsStStbprpB.getStnm());
                }
                if (bdmsStStbprpB.getStct() != null && !"".equals(bdmsStStbprpB.getStct())) {
                    sationInfo.setStct(bdmsStStbprpB.getStct());
                }
                if (bdmsStStbprpB.getSttp() != null && !"".equals(bdmsStStbprpB.getSttp())) {
                    sationInfo.setSttp(bdmsStStbprpB.getSttp());
                }
                if (bdmsStStbprpB.getAbsttp() != null && !"".equals(bdmsStStbprpB.getAbsttp())) {
                    sationInfo.setAbsttp(bdmsStStbprpB.getAbsttp());
                }
                if (bdmsStStbprpB.getProv() != null && !"".equals(bdmsStStbprpB.getProv())) {
                    sationInfo.setProvice(bdmsStStbprpB.getProv());
                }
                if (bdmsStStbprpB.getCity() != null && !"".equals(bdmsStStbprpB.getCity())) {
                    sationInfo.setCity(bdmsStStbprpB.getCity());
                }
                if (bdmsStStbprpB.getCls() != null) {
                    sationInfo.setCls(bdmsStStbprpB.getCls());
                }
                if (bdmsStStbprpB.getStgrd() != null) {
                    sationInfo.setStgrd(bdmsStStbprpB.getStgrd());
                }
                if (bdmsStStbprpB.getAreaname() != null && !"".equals(bdmsStStbprpB.getAreaname())) {
                    sationInfo.setAreaName(bdmsStStbprpB.getAreaname());
                }
                if (bdmsStStbprpB.getRemark() != null && !"".equals(bdmsStStbprpB.getRemark())) {
                    sationInfo.setRemarks(bdmsStStbprpB.getRemark());
                }
                List<BDMS_ST_SENSOR_B> bdmsStSensorBs = configList.stream().filter(x -> x.getStcd().equals(bdmsStStbprpB.getStcd()))
                        .collect(Collectors.toList());
                if (bdmsStSensorBs.size() > 0) {
                    List<SensorConfig> sensorConfigs = new ArrayList<>();
                    SensorConfig sensorConfig = null;
                    for (BDMS_ST_SENSOR_B bdmsStSensorB : bdmsStSensorBs) {
                        sensorConfig = new SensorConfig();
                        if (bdmsStSensorB.getSensortps() != null && !"".equals(bdmsStSensorB.getSensortps())) {
                            sensorConfig.setSensorType(bdmsStSensorB.getSensortps());
                        }
                        if (bdmsStSensorB.getSensornm() != null && !"".equals(bdmsStSensorB.getSensornm())) {
                            sensorConfig.setSensorName(bdmsStSensorB.getSensornm());
                        }
                        if (bdmsStSensorB.getDatacode() != null && !"".equals(bdmsStSensorB.getDatacode())) {
                            sensorConfig.setDataCode(bdmsStSensorB.getDatacode());
                        }
                        if (bdmsStSensorB.getVallowlmt() != null) {
                            sensorConfig.setValLowLmt(bdmsStSensorB.getVallowlmt());
                        }
                        if (bdmsStSensorB.getValuplmt() != null) {
                            sensorConfig.setValUpLmt(bdmsStSensorB.getValuplmt());
                        }
                        if (bdmsStSensorB.getMxtm() != null && !"".equals(bdmsStSensorB.getMxtm())) {
                            sensorConfig.setStartTime(bdmsStSensorB.getMxtm());
                        }
                        if (bdmsStSensorB.getMntm() != null && !"".equals(bdmsStSensorB.getMntm())) {
                            sensorConfig.setEndTime(bdmsStSensorB.getMntm());
                        }
                        sensorConfigs.add(sensorConfig);
                        sationInfo.setSensorConfigs(sensorConfigs);
                    }
                }
                sationInfos.add(sationInfo);
            }
            return sationInfos;
        }else {
            return null;
        }
    }

    @Override
    public BDMS_ST_STBPRP_B selsctByPidAndStcd(int projectId, String stcd) {
        return bdms_st_stbprp_bMapper.selsctByPidAndStcd(projectId,stcd);
    }

    @Override
    public List<BDMS_ST_STBPRP_B> selectByCity() {
        return bdms_st_stbprp_bMapper.selectByCity();
    }
}
